package com.c.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.c.entity.CheckGroup;
import com.c.entity.SportGroup;
import com.c.entity.SportItem;
import com.c.service.SportGroupService;
import com.c.service.SportItemService;
import com.c.service.UserService;
import com.c.utils.MessageConstant;
import com.c.utils.PageResult;
import com.c.utils.QueryPageBean;
import com.c.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wangxiangli
 * @create 2022-12-26-15:23
 */
@RestController
@RequestMapping("/sportgroup")
public class SportGroupController {

    @Autowired
    private SportGroupService sportGroupService;

    @Autowired
    private UserService userService;

    /**
     * 运动项分页查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = sportGroupService.pageQuery2(queryPageBean);
        return pageResult;
    }


    /**
     * 新增运动方案
     *
     * @param sportGroup
     * @param sportitemIds
     * @return
     */
    @RequestMapping("add")
    //@RequestBody 解析提交的json，封装成对象
    public Result add(@RequestBody SportGroup sportGroup,Integer[] sportitemIds) {
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            QueryWrapper<com.c.entity.User> wrapper=new QueryWrapper<>();
            wrapper.eq("username",user.getUsername());
            com.c.entity.User user1 = userService.getOne(wrapper);
            sportGroup.setCreateId(user1.getId());
            sportGroup.setUpdateId(user1.getId());
            sportGroupService.add(sportGroup, sportitemIds);
            return new Result(true, MessageConstant.COMMON_ADD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.COMMON_ADD_FAILE);
        }
    }

    /**
     * 根据ID查询运动组
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            SportGroup sportGroup = sportGroupService.getById(id);
            return new Result(true, MessageConstant.QUERY_COMMONSUCCESS, sportGroup);//查询成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_COMMONFAILE);//查询失败
        }
    }

    /**
     * 根据运动组ID查询运动组包含的多个运动项ID
     *
     * @param id
     * @return
     */
    @RequestMapping("/findSportItemIdsBySportGroupId")
    public Result findSportItemIdsBySportGroupId(Integer id) {
        try {
            List<Integer> sportitemIds = sportGroupService.findSportItemIdsBySportGroupId(id);
            return new Result(true, MessageConstant.QUERY_COMMONSUCCESS, sportitemIds);//查询成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_COMMONFAILE);//查询失败
        }
    }

    /**
     * 编辑运动组
     *
     * @param sportGroup
     * @param sportitemIds
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody SportGroup sportGroup, Integer[] sportitemIds) {
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            QueryWrapper<com.c.entity.User> wrapper=new QueryWrapper<>();
            wrapper.eq("username",user.getUsername());
            com.c.entity.User user1 = userService.getOne(wrapper);
            sportGroup.setUpdateId(user1.getId());
            sportGroupService.edit(sportGroup, sportitemIds);
            return new Result(true, MessageConstant.COMMON_EDIT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.COMMON_EDIT_FAILE);
        }
    }



    /**
     * 删除运动组
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            return sportGroupService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.COMMON_DELETE_FAILE);
        }
    }

}
